package com.itwn.portal.web;

import com.itwn.portal.reult.ResponseResult;
import com.itwn.portal.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("collection" )
public class CollectionController {
    @Autowired
    CollectionService collectionService;
    /**
     * 添加收藏夹
     * @param uid
     * @param rid
     * @return
     */
    @GetMapping("/add")
    public ResponseResult<Void> add (Integer uid , Integer rid){
        System.out.println("添加收藏："+uid+":"+rid);
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            collectionService.addCollection(uid,rid);
            result.setCode(200);
            result.setMsg("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return result;
    }

    /**
     * 移出收藏夹
     * @param uid
     * @param rid
     * @return
     */
    @GetMapping("/remove")
    public ResponseResult<Void> remove (Integer uid , Integer rid){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            collectionService.removeCollection(uid,rid);
            result.setCode(200);
            result.setMsg("移出成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("移出失败");
        }
        return result;
    }

    @RequestMapping("/isCol")
    @ResponseBody
    public ResponseResult<Integer> isCol(Integer uid,Integer rid){
        System.out.println("你好，我是判断收藏"+uid+":"+rid);
        ResponseResult<Integer> result = new ResponseResult<>();
        try{
            int isCol = this.collectionService.isCol(rid,uid);
            System.out.println("判断收藏数据为:"+isCol);
            result.setCode(200);
            result.setMsg("判断成功！");
            result.setData(isCol);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("判断失败！");
        }
        return result;
    }

}
